package com.example.webapptweet;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.web_view);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);       // 这个要加上

        String nickname = "lengJing";                        // 用户的nickname
        String headimgurl = "http://wx.qlogo.cn/mmopen/eBhXuS1y3rCIEeLp5YtNHLISibBfee8aNHoFKA7RbjgXQB6LB8YpiaTR6XOibwZNRv5neg4dYnfnibITanHzibk6W6nDFm73etJTm/0";  // 用户的头像url
        String openid = "121368577";

        String url = "https://support.qq.com/product/34122";
        /* 准备post参数 */
        String postData = "nickname=" + nickname + "&avatar="+ headimgurl + "&openid=" + openid;
        webView.postUrl(url, postData.getBytes());

/* 获得 webview url,请注意下面url单词是product而不是products,products是旧版本用错地址将不能成功提交 */

//        webView.loadUrl(url);
        WebViewClient webViewClient = new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                super.shouldOverrideUrlLoading(view, url);

                if (url == null) {
                    return  false;
                }
                try {
                    if (url.startsWith("weixin://")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        view.getContext().startActivity(intent);
                        return true;
                    }
                } catch (Exception e) {
                    return false;
                }
                view.loadUrl(url);
                return true;
            }
        };
        webView.setWebViewClient(webViewClient);
    }
}
